package com.neusoft.yuejian12.yuejian_12.model;

import com.neusoft.yuejian12.yuejian_12.javabean.AttentionBean;
import com.neusoft.yuejian12.yuejian_12.javabean.EnterTrainBean;
import com.neusoft.yuejian12.yuejian_12.javabean.IsAttentionBean;
import com.neusoft.yuejian12.yuejian_12.javabean.TrainBean;

import com.neusoft.yuejian12.yuejian_12.listener.RetrofitListener;
import com.neusoft.yuejian12.yuejian_12.service.TrainService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.BASE_URL;



/**
 * Created by Administrator on 2017/9/25.
 */

public class TrainModel{
    private Retrofit retrofit;
    public TrainModel(){
        //创建实例
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //培训列表
    public void getTrainlist(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlist();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    public void getTrainlista(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlista();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    public void getTrainlistb(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistb();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    public void getTrainlistc(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistc();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    public void getTrainlistd(final RetrofitListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.getTrainlistd();
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainBeen = response.body();
                listener.onSuccess(trainBeen);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }


    //培训详情
    public Call<TrainBean> getDetail(int t_id){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.getDetail(t_id);
    }

    //搜索培训
    public Call<List<TrainBean>> searchTrain(String t_name){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.searchTrain(t_name);
    }

//    public void searchTrain(String t_name,final View.OnClickListener listener){
//        TrainService trainService = retrofit.create(TrainService.class);
//        Call call = trainService.searchTrain(t_name);
//        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
//            @Override
//            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
//                List<TrainBean> trainBean = response.body();
//                listener.onSuccess(trainBean);
//            }
//            @Override
//            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
//                listener.onFail();
//            }
//        };
//        call.enqueue(callback);
//
//    }

    //报名培训
    public Call<EnterTrainBean> addtrainn(int u_id, int t_id){
        TrainService trainService = retrofit.create(TrainService.class);
        return trainService.addtrainn(u_id,t_id);
    }

    //判断是否关注
    public Call<IsAttentionBean> getIsCollected(int u_id, int select_id, String flag){
        TrainService trainService=retrofit.create(TrainService.class);
        return trainService.getIsCollected(u_id, select_id, flag);
    }

    //用户关注培训接口
    public Call<AttentionBean> userCollectTrain(int u_id, int t_id){
        TrainService trainService=retrofit.create(TrainService.class);
        return trainService.userCollectTrain(u_id,t_id);
    }

}
